package main

import (
	"fmt"
	"math"
	"math/rand"
	"time"
)

//二分查找
func binaryFind(a [10]int, lindex, rindex int, v int) {
	if lindex > rindex {
		fmt.Println("查无此数")
		return
	}
	m := (lindex + rindex) / 2
	if a[m] > v {
		binaryFind(a, lindex, m-1, v)
	} else if a[m] < v {
		binaryFind(a, m+1, rindex, v)
	} else {
		fmt.Println("此数的下标为:", m, "值为:", a[m])
	}
}

//数组求值
func test1() {
	rand.Seed(time.Now().Unix())
	var a [10]int
	//生成指定范围随机数需要自己+min
	// min := 5
	// max := 10
	// for i := 0; i <= 3; i++ {
	// 	fmt.Println(rand.Intn(max-min+1) + min)
	// }
	for i := 0; i < 10; i++ {
		a[i] = rand.Intn(101)
	}
	fmt.Println(a)
	for j := 0; j < len(a)-1; j++ {
		for i := 0; i < len(a)-1-j; i++ {
			if a[i] > a[i+1] {
				a[i], a[i+1] = a[i+1], a[i]
			}
		}
	}
	fmt.Println(a)
	var sum int
	for i := 0; i < len(a); i++ {
		fmt.Printf("%v\t", a[len(a)-i-1])
		sum += a[i]
	}
	fmt.Println()
	fmt.Printf("随机数平均值为：%.1f\n", float64(sum)/float64(len(a)))
	binaryFind(a, 0, len(a)-1, 55)
}

//插入元素升序排序
func test2(v int) {
	var a [10]int
	for i := 0; i < 10; i++ {
		a[i] = rand.Intn(101)
	}

	for j := 0; j < len(a)-1; j++ {
		for i := 0; i < len(a)-1-j; i++ {
			if a[i] > a[i+1] {
				a[i], a[i+1] = a[i+1], a[i]
			}
		}
	}
	fmt.Println(a)

	var b [len(a) + 1]int
	fmt.Println(b)
	for i := 0; i < len(a); i++ {
		if v > a[i] {
			b[i] = a[i]
		} else if v <= a[i] {
			b[i] = v
			for j := i; j < len(a); j++ {
				b[j+1] = a[j]
			}
			fmt.Println(b)
			return
		}
	}
}

//二维数组清0
func test3() {
	rand.Seed(time.Now().Unix())
	var a [3][4]int
	for i := 0; i < 3; i++ {
		for j := 0; j < 4; j++ {
			a[i][j] = rand.Intn(101)
		}
	}
	fmt.Println(a)
	for i := 0; i < 3; i++ {
		for j := 0; j < 4; j++ {
			if i == 0 || j == 0 || i == 2 || j == 3 {
				a[i][j] = 0
			}

		}
	}
	fmt.Println(a)
}

//评委打分
func test10() {
	var a [8]int
	rand.Seed(time.Now().Unix())
	for i := 0; i < len(a); i++ {
		a[i] = rand.Intn(101)
	}
	fmt.Println(a)
	var sum int
	high := 1
	low := 1
	//最高分评委
	for i := 0; i < len(a)-1; i++ {
		if a[high-1] < a[i+1] {
			high = i + 2
		}
		sum += a[i]
	}
	fmt.Println("8位评委总分:", sum)
	fmt.Printf("第%v位评委给了最高分,分数为:%v\n", high, a[high-1])
	//最低分评委
	for i := 0; i < len(a)-1; i++ {
		if a[low-1] > a[i+1] {
			low = i + 2
		}
	}
	fmt.Printf("第%v位评委给了最低分,分数为:%v\n", low, a[low-1])

	avg := math.Round(float64(sum-a[high-1]-a[low-1]) / float64(6))
	fmt.Printf("%v\n", avg)
	//最佳评委
	best := 1
	for i := 0; i < len(a)-1; i++ {
		if math.Abs(float64(a[best-1])-avg) > math.Abs(float64(a[i+1])-avg) {
			best = i + 2
		}
	}
	fmt.Printf("第%v位评委是最佳评委,分数为:%v\n", best, a[best-1])
	//最差评委
	bad := 1
	for i := 0; i < len(a)-1; i++ {
		if math.Abs(float64(a[bad-1])-avg) < math.Abs(float64(a[i+1])-avg) {
			bad = i + 2
		}
	}
	fmt.Printf("第%v位评委是最佳评委,分数为:%v\n", bad, a[bad-1])
}

func main() {
	test1()
	test2(45)
	test3()
	test10()
}
